package com.example.springbootexamples.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class T {
    @Test
    public void t(){
        SecurityUtils.setSecurityManager(new DefaultSecurityManager(new AuthorizingRealm() {
            @Override
            protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                Set<String> roles=new HashSet<>();
                roles.add("admin");
                SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo(roles);
                Collection<String> collection=new ArrayList<>();
                collection.add("a:b:c");
                simpleAuthorizationInfo.addStringPermissions(collection);
                return simpleAuthorizationInfo;
            }

            @Override
            protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
                UsernamePasswordToken usernamePasswordToken= (UsernamePasswordToken) token;
                if(usernamePasswordToken.getUsername().equals("admin")&&new String(usernamePasswordToken.getPassword()).equals("pass")){
                    return new SimpleAuthenticationInfo(usernamePasswordToken.getUsername(),usernamePasswordToken.getPassword(),"hello");
                }else {
                    throw new AuthenticationException("pass err");
                }

            }
        }));
        Subject currentUser = SecurityUtils.getSubject();

        Session session = currentUser.getSession();
        session.setAttribute( "someKey", "aValue" );

        if ( !currentUser.isAuthenticated() ) {
            //collect user principals and credentials in a gui specific manner
            //such as username/password html form, X509 certificate, OpenID, etc.
            //We'll use the username/password example here since it is the most common.
            //(do you know what movie this is from? ;)
            UsernamePasswordToken token = new UsernamePasswordToken("admin", "pass");
            //this is all you have to do to support 'remember me' (no config - built in!):
            token.setRememberMe(true);
            try {
                currentUser.login(token);
                currentUser.hasRole("a");
                boolean b=currentUser.hasRole("admin");
                boolean b1=currentUser.isPermitted("a:b:1");
                boolean b2=currentUser.isPermitted("a:b:c");
                System.out.println("1234");
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
